<template>
  <div class="role">
    <el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="40%">
      <el-form :model="role" :rules="rules" ref="role" label-width="100px" class="demo-ruleForm">
        <input type="hidden" value="role.id" />
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="role.roleName"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" :rows="3" v-model="role.remark"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('role')">确定</el-button>
          <el-button @click="resetForm('role')">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- </el-card> -->
    </el-dialog>
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card class="box-card">
          <div class="boxcard-head">
            <el-form :model="queryParam" ref="queryParam" inline class="demo-form-inline">
              <el-form-item label="角色名称" prop="name">
                <el-input placeholder="角色名称" v-model="queryParam.name"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
                <el-button @click="resetForm('queryParam')">重置</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="addRole">添加</el-button>
              </el-form-item>
            </el-form>
          </div>
          <div class="boxcard-body">
            <el-table
              :data="list"
              style="width: 100%;"
              row-key="id"
              :header-cell-style="{ 'background-color': '#EEEEEE' }"
            >
              <el-table-column prop="id" label="ID"></el-table-column>
              <el-table-column prop="roleName" label="角色名称"></el-table-column>
              <el-table-column prop="remark" label="公司备注"></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button type="warning" size="small" @click="updateRole(scope.row)">修改</el-button>
                  <el-button type="warning" size="small" @click="authResource(scope.row)">分配权限</el-button>
                  <!-- <el-button type="danger" size="small" @click="deleteRole(scope.row)">删除</el-button> -->
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="pagination">
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page.currentPage"
              :page-sizes="page.pageSizes"
              :page-size="page.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
            ></el-pagination>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <role-authority
      :dialog-visible="roleAuthorityDialog.isVisible"
      @close="roleAuthorityClose"
      @success="roleAuthoritySuccess"
      ref="roleAuthority"
    />
  </div>
</template>

<script>
import RoleAuthority from "./RoleAuthority";
import { get, postJson, postXform } from "@/utils/request.js";
export default {
  components: { RoleAuthority },
  data() {
    return {
      dialog: {
        title: "添加角色",
        visible: false
      },
      list: [],
      rules: {},
      queryParam:{
        name:null
      },
      page: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
        pageSizes: [10, 20, 50]
      },
      role: { id: "", roleName: "", remark: "" },
      roleAuthorityDialog: {
        isVisible: false
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.getList();
    },
    updateRole(row) {
      this.dialog.visible = true;
      this.dialog.title = "修改角色";
      this.role = row;
    },
    deleteRole(row) {},
    addRole() {
      this.dialog.visible = true;
      this.dialog.title = "添加角色";
      this.role = { id: "", roleName: "", remark: "" };
    },
    searchRole() {},
    getList() {
      get("/role/pageRole", {
        pageNum: this.page.currentPage,
        pageSize: this.page.pageSize,
        name:this.queryParam.name
      }).then(res => {
        this.list = res.data.data.list;
        this.page.total = Number.parseInt(res.data.data.total);
        this.page.currentPage = res.data.data.pageNum;
      });
    },
    submitForm(role) {
      this.$refs.role.validate(valid => {
        if (valid) {
          if (this.role.id === "") {
            postXform("/role/addRole", this.role).then(res => {
              this.dialog.visible = false;
              this.$message({ message: "添加成功", type: "success" });
              this.getList();
            });
          } else {
            postXform("/role/updateRole", this.role).then(res => {
              this.dialog.visible = false;
              this.$message({ message: "修改成功", type: "success" });
              this.getList();
            });
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      if (formName == "queryParam") {
        this.getList();
      }
    },
    roleAuthorityClose() {
      this.roleAuthorityDialog.isVisible = false;
    },
    roleAuthoritySuccess() {
      this.getList();
    },
    authResource(row) {
      this.roleAuthorityDialog.isVisible = true;
      this.$refs.roleAuthority.setRoleAuthority(row);
    }
  }
};
</script>

<style lang="less">
</style>
